package com.nest4cloud.nest.account.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nest4cloud.nest.common.core.util.R;
import com.nest4cloud.nest.common.log.annotation.SysLog;
import com.nest4cloud.nest.common.security.util.SecurityUtils;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.nest4cloud.nest.account.entity.AccountsEntity;
import com.nest4cloud.nest.account.service.AccountsService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.nest4cloud.nest.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 账单表
 *
 * @author nest
 * @date 2024-11-24 21:37:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accAccounts" )
@Tag(description = "accAccounts" , name = "账户表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccountsController {

    private final  AccountsService accountsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param accounts 账单表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("account_accAccounts_view")
    public R getAccountsPage(@ParameterObject Page page, @ParameterObject AccountsEntity accounts) {
        LambdaQueryWrapper<AccountsEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(accounts.getAccountName()),AccountsEntity::getAccountName,accounts.getAccountName());
		wrapper.eq(StrUtil.isNotBlank(accounts.getAccountNumber()),AccountsEntity::getAccountNumber,accounts.getAccountNumber());
     		wrapper.eq(Objects.nonNull(accounts.getCreateTime()),AccountsEntity::getCreateTime,accounts.getCreateTime());
     		wrapper.eq(AccountsEntity::getUserId, SecurityUtils.getUser().getId());
        return R.ok(accountsService.pageEntity(page, wrapper));
    }


    /**
     * 通过条件查询账单表
     * @param accounts 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("account_accAccounts_view")
    public R getDetails(@ParameterObject AccountsEntity accounts) {
        return R.ok(accountsService.list(Wrappers.query(accounts)));
    }


	/**
	 * 查询账单总金额-用户
	 * @param accounts 查询条件
	 * @return R  对象列表
	 */
	@Operation(summary = "查询账单总金额" , description = "查询账单总金额" )
	@GetMapping("/totalAmount" )
	public R getTotalAmount(@ParameterObject AccountsEntity accounts) {
		return R.ok(accountsService.getTotalAmount(accounts));
	}

	@Operation(summary = "用户账户查询" , description = "查询登录用户的账户信息")
	@GetMapping("/userAccount")
	public R getUserAccount() {
		return R.ok(accountsService.getUserAccount());
	}



    /**
     * 新增账单表
     * @param accounts 账单表
     * @return R
     */
    @Operation(summary = "新增账户表" , description = "新增账户表" )
    @SysLog("新增账户表" )
    @PostMapping
    @HasPermission("account_accAccounts_add")
    public R save(@RequestBody AccountsEntity accounts) {
        return R.ok(accountsService.saveAccount(accounts));
    }

    /**
     * 修改账单表
     * @param accounts 账单表
     * @return R
     */
    @Operation(summary = "修改账单表" , description = "修改账单表" )
    @SysLog("修改账单表" )
    @PutMapping
    @HasPermission("account_accAccounts_edit")
    public R updateById(@RequestBody AccountsEntity accounts) {
        return R.ok(accountsService.updateById(accounts));
    }

    /**
     * 通过id删除账单表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除账单表" , description = "通过id删除账单表" )
    @SysLog("通过id删除账单表" )
    @DeleteMapping
    @HasPermission("account_accAccounts_del")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(accountsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param accounts 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("account_accAccounts_export")
    public List<AccountsEntity> exportExcel(AccountsEntity accounts,Integer[] ids) {
        return accountsService.list(Wrappers.lambdaQuery(accounts).in(ArrayUtil.isNotEmpty(ids), AccountsEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param accountsList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("account_accAccounts_export")
    public R importExcel(@RequestExcel List<AccountsEntity> accountsList, BindingResult bindingResult) {
        return R.ok(accountsService.saveBatch(accountsList));
    }
}
